// 新建处理文章的数据模型
// let mysql = require('mysql');
// let connection = mysql.createConnection({
//     host: 'localhost',
//     user: 'root',
//     password: '123',
//     database: 'blog'
// });
// connection.connect();
let connection = require('./dbHelpter');

module.exports = {
    // 添加文章
    addArticle: (newArticle, cb) => {
        let sql = 'insert into articles set ?';
        connection.query(sql, newArticle, (err, results) => {
            if (err) return cb(err, null);
            cb(null, results);
        });
    },
    // 新建查询文章详情及用户的nick的model函数
    getDetail: (articleId, cb) => {
        // 写查询的sql语句
        let sql = 'select articles.*,users.nickname from articles LEFT JOIN users on articles.userId=users.id where articles.id=?';
        connection.query(sql, [articleId], (err, results) => {
            if (err) return cb(err, null);
            cb(null, results);
        });
    },
    // 新建查询文章信息的model
    queryArticleById: (articleId, cb) => {
        let sql = 'select * from articles where id=?';
        connection.query(sql, [articleId], (err, results) => {
            if (err) return cb(err, null);
            cb(null, results);
        });
    },
    // 编辑文章的model
    updateArticleById: (article, cb) => {
        let sql = 'update articles set title=?,content=?,ctime=? where id=?';
        connection.query(sql, [article.title, article.id], (err, results) => {
            if (err) return cb(err, null);
            cb(null, results);
        });
    }
}